//
//  filter_iPhone_ViewController.m
//  chilemat
//
//  Created by Cristian Gonzalez Figueroa on 28-03-14.
//  Copyright (c) 2014 Chilemat SpA. All rights reserved.
//

#import "filter_iPad_ViewController.h"
#import "classGenericCell.h"
#import "CustomCell.h"
#import "dao.h"

@interface filter_iPad_ViewController ()

@end

@implementation filter_iPad_ViewController

@synthesize viewApplyFilter, lblPriceFrom, lblPriceTo, lblPrice;
@synthesize objSliderPriceFilter;
@synthesize tableViewSearch;
@synthesize dataArraySearch = _dataArraySearch;
@synthesize stateIndex = _stateIndex;
@synthesize listOfMarc = _listOfMarc;
@synthesize tableViewOrder;
@synthesize dataArrayOrder = _dataArrayOrder;
@synthesize segmentedControl;
@synthesize json;
@synthesize jsonMarcas;

- (void)hideTabBar {
    UITabBar *tabBar = self.tabBarController.tabBar;
    UIView *parent = tabBar.superview; // UILayoutContainerView
    UIView *content = [parent.subviews objectAtIndex:0];  // UITransitionView
    UIView *window = parent.superview;
    
    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect tabFrame = tabBar.frame;
                         tabFrame.origin.y = CGRectGetMaxY(window.bounds);
                         tabBar.frame = tabFrame;
                         content.frame = window.bounds;
                     }];
}

- (void)showTabBar {
    UITabBar *tabBar = self.tabBarController.tabBar;
    UIView *parent = tabBar.superview; // UILayoutContainerView
    UIView *content = [parent.subviews objectAtIndex:0];  // UITransitionView
    UIView *window = parent.superview;
    
    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect tabFrame = tabBar.frame;
                         tabFrame.origin.y = CGRectGetMaxY(window.bounds) - CGRectGetHeight(tabBar.frame);
                         tabBar.frame = tabFrame;
                         
                         CGRect contentFrame = content.frame;
                         contentFrame.size.height -= tabFrame.size.height;
                     }];
}

//----------------------------------------------------------------------------------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == tableViewSearch) {
        return [_stateIndex count];
    }else{
        return 1;
    }
    
}

//---set the index for the table---
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    NSArray *stateIndexTmp;
    
    if (tableView == tableViewSearch) {
        return _stateIndex;
    }else{
        return stateIndexTmp;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == tableViewSearch) {
        //---get the letter in each section; e.g., A, B, C, etc.---
        NSString *alphabet = [_stateIndex objectAtIndex:section];
        
        //---get all states beginning with the letter---
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
        NSArray *alphabetContacts = [_listOfMarc filteredArrayUsingPredicate:predicate];
        
        //---return the number of states beginning with the letter---
        return [alphabetContacts count];
        
    }else{
        return [_dataArrayOrder count];
    }
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (tableView == tableViewOrder) {
        return @"";
        
    }else{
        return [_stateIndex objectAtIndex:section];
    }
}

//----------------------------------------------------------------------------------------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:CellIdentifier];
    }
    
    if (tableView == tableViewSearch) {
        
        NSString *alphabet = [_stateIndex objectAtIndex:[indexPath section]];
        
        //---get all states beginning with the letter---
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
        NSArray *fullContactsNameArray = [_listOfMarc filteredArrayUsingPredicate:predicate];
        
        if ([fullContactsNameArray count]>0) {
            //---extract the relevant state from the states object---
            NSString *cellValue = [fullContactsNameArray objectAtIndex:indexPath.row];
            
            //cell.textLabel.text = cellValue;
            
            for (int i=0; i<[_dataArraySearch count]; i++){
                classGenericCell *objCell = [_dataArraySearch objectAtIndex:i];
                
                if (objCell.strTitleLabel == cellValue) {
                    cell.lblTitleLabel.text = cellValue; //currentContact.fullName; //[_dataArrayTitleF objectAtIndex:indexPath.row];
                    
                    NSLog(@"cell.lblTitleLabel.text=%@'", cell.lblTitleLabel.text);
                    
                    if ([cell.lblTitleLabel.text  isEqual: @"Ceresita"]) {
                        
                        //cell.selected = YES;
                        //cell.backgroundColor = [UIColor lightGrayColor];
                    }
                    
                }
            }
        }
        
    }else{
        classGenericCell *objCell = [_dataArrayOrder objectAtIndex:indexPath.row];
        cell.lblTitleLabel.text = objCell.strTitleLabel;
    }
    
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    dao *objDAO = [[dao alloc] init];
    
    int intIdUsuario = [objDAO obtainCurrentIdUser];
    
    if (intIdUsuario == -1) {
        intIdUsuario = 1;
    }
    
    int intIdRow = (int) indexPath.row;
    int intIdSection = (int) indexPath.section;
    
    NSLog(@"intIdRow = %i", intIdRow);
    NSLog(@"intIdSection = %i", intIdSection);
    
    if (tableView == tableViewOrder) {
        NSLog(@"tableViewOrder Selected");
        
        [objDAO deleteUserTipoOrdenamiento:intIdUsuario];
        /*
         1.- Nombre A - Z
         2.- Nombre Z - A
         3.- Mayor precio
         4.- Menor precio
         5.- Marca
         6.- Mas populares
         */
        
        [objDAO insertUserTipoOrdenamiento:intIdUsuario intIdTipoOrdenamiento:intIdRow + 1 intIdRow:intIdRow intIdSection:intIdSection];
        
    }else{
        //------------
        NSString *alphabet = [_stateIndex objectAtIndex:[indexPath section]];
        
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet];
        NSArray *fullContactsNameArray = [_listOfMarc filteredArrayUsingPredicate:predicate];
        
        if ([fullContactsNameArray count]>0) {
            //---extract the relevant state from the states object---
            NSString *cellValue = [fullContactsNameArray objectAtIndex:indexPath.row];
            
            for (int i=0; i<[_dataArraySearch count]; i++){
                classGenericCell *currentMarc = [_dataArraySearch objectAtIndex:i];
                
                if (currentMarc.strTitleLabel == cellValue) {
                    
                    NSLog(@"strIdMarc=%@", currentMarc.strIdMark);
                    NSLog(@"currentMarc.strTitleLabel=%@", currentMarc.strTitleLabel);
                    
                    int intIdMarc = [currentMarc.strIdMark intValue];
                    
                    [objDAO deleteUserFilterMarc:intIdUsuario];
                    
                    [objDAO insertUserFilterMarc:intIdUsuario intIdMarc:intIdMarc intIdRow:intIdRow intIdSection:intIdSection];
                }
            }
        }
        //------------
    }
}

- (void) retrieveDataOrder{
    
    NSString *strMarca = @"";
    
    [_dataArrayOrder removeAllObjects];
    _dataArrayOrder = [[NSMutableArray alloc] init];
    
    for (int i = 0; i<6; i++)
    {
        if (i == 0) {
            strMarca = @"Nombre A - Z";
        }
        if (i == 1) {
            strMarca = @"Nombre Z - A";
        }
        if (i == 2) {
            strMarca = @"Mayor precio";
        }
        if (i == 3) {
            strMarca = @"Menor precio";
        }
        if (i == 4) {
            strMarca = @"Marca";
        }
        if (i == 5) {
            strMarca = @"Mas Populares";
        }
        
        NSString *strTitleLabel = [NSString stringWithFormat:@"%@", strMarca];
        NSString *strSubtitleLabel = @"";
        NSString *strTrademark = @"";
        NSString *strIdmark = [NSString stringWithFormat:@"%i", i];
        
        NSString *strSKU = @"";
        NSString *strIdProducto = [NSString stringWithFormat:@"%i" , i];
        NSString *strReviews = @"";
        NSString *strPrice = @"";
        NSString *strOfferPrice = @"";
        NSString *strPriceDuplicated = @"";
        NSString *strDiscount = @"";
        NSString *strOfferTime = @"";
        NSString *strQty = @"";
        
        classGenericCell *objCell = [[classGenericCell alloc] initClassCell:strTitleLabel andSubtitleLabel:strSubtitleLabel andTrademark:strTrademark andIdMark:strIdmark andSKU:strSKU andStrIdProducto:strIdProducto andReviews:strReviews andstrPrice:strPrice andOfferPrice:strOfferPrice andstrPriceDuplicated:strPriceDuplicated andDiscount:strDiscount andOfferTime:strOfferTime andQty:strQty];
        
        [_dataArrayOrder addObject:objCell];
    }
    
    [self.tableViewOrder reloadData];
}

- (IBAction)changeFilterOptions:(id)sender {
    
    switch (self.segmentedControl.selectedSegmentIndex) {
        case 0:{
            /* Filter */
            NSLog(@"segmentedControlIndexChanged Index 0...");
            
            self.tableViewOrder.hidden = YES;
            self.tableViewSearch.hidden = YES;
            self.viewPriceFilter.hidden = NO;
            
            break;
        }
        case 1:{
            /* Order */
            NSLog(@"segmentedControlIndexChanged Index 1...");
            
            self.tableViewOrder.hidden = NO;
            self.tableViewSearch.hidden = YES;
            
            //          [self retrieveDataOrder];
            
            //-------------------------------------------------------------------------------------------------------------
            dao *objDAO = [[dao alloc] init];
            
            int intIdUsuario = [objDAO obtainCurrentIdUser];
            
            if (intIdUsuario == -1) {
                intIdUsuario = 1;
            }
            
            if ([objDAO obtainUserTipoOrdenamiento:intIdUsuario] > 0 ) {
                
                //-----------------------------------------------------------------
                //Marcamos la fila seleccionada en el sistema en base a row y section de indexPath
                NSArray *arrayFilterMarc = [objDAO obtainUserTipoOrdenamientoData:intIdUsuario];
                
                NSLog(@"arrayFilterPrice[0]: %@", arrayFilterMarc[0]);
                NSLog(@"arrayFilterPrice[1]: %@", arrayFilterMarc[1]);
                NSLog(@"arrayFilterPrice[2]: %@", arrayFilterMarc[2]);
                
                //              strTipoOrd = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0 )];
                //              strIdRow = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1 )];
                //              strIdSection = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,2 )];
                
                NSInteger intIdRow = [arrayFilterMarc[1] integerValue];
                NSInteger intIdSection = [arrayFilterMarc[2] integerValue];
                
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:intIdRow inSection:intIdSection];
                
                [tableViewOrder selectRowAtIndexPath:indexPath
                                            animated:NO
                                      scrollPosition:UITableViewScrollPositionNone];
            }
            //---------------------------------------------------------------------------------------------------------------
            
            
            break;
        }
    }
}


/*
- (void) retrieveDataMarcas{
    
    NSString *strMarca = @"";
    
    [_dataArraySearch removeAllObjects];
    _dataArraySearch = [[NSMutableArray alloc] init];
    
    [_listOfMarc removeAllObjects];
    _listOfMarc = [[NSMutableArray alloc] init];
    
    for (int i = 0; i<13; i++)
    {
        if (i == 0) {
            strMarca = @"Black and Decker";
        }else if (i == 1){
            strMarca = @"Ceresita";
        }else if (i == 2){
            strMarca = @"Vinilit";
        }else if (i == 3){
            strMarca = @"Tricolor";
        }else if (i == 4){
            strMarca = @"Arauco";
        }else if (i == 5){
            strMarca = @"Crossman";
        }else if (i == 6){
            strMarca = @"Makita";
        }else if (i == 7){
            strMarca = @"Junkers";
            
        }else if (i == 8){
            strMarca = @"Dideval";
            
        }else if (i == 9){
            strMarca = @"Electrolux";
            
        }else if (i == 10){
            strMarca = @"Fiorucci";
            
        }else if (i == 11){
            strMarca = @"Guante";
            
        }else if (i == 12){
            strMarca = @"Hidrofil";
            
        }else if (i == 13){
            strMarca = @"Ilufilm";
            
        }else{
            strMarca = @"Zibel";
        }
        
        NSString *strTitleLabel = [NSString stringWithFormat:@"%@", strMarca];
        NSString *strSubtitleLabel = @"";
        NSString *strTrademark = @"";
        NSString *strIdmark = [NSString stringWithFormat:@"%i", i];
        
        NSString *strSKU = @"";
        NSString *strIdProducto = [NSString stringWithFormat:@"%i" , i];
        NSString *strReviews = @"";
        NSString *strPrice = @"";
        NSString *strOfferPrice = @"";
        NSString *strPriceDuplicated = @"";
        
        NSString *strDiscount = @"";
        NSString *strOfferTime = @"";
        NSString *strQty = @"";
        
        classGenericCell *objCell = [[classGenericCell alloc] initClassCell:strTitleLabel andSubtitleLabel:strSubtitleLabel andTrademark:strTrademark andIdMark:strIdmark andSKU:strSKU andStrIdProducto:strIdProducto andReviews:strReviews andstrPrice:strPrice andOfferPrice:strOfferPrice andstrPriceDuplicated:strPriceDuplicated andDiscount:strDiscount andOfferTime:strOfferTime andQty:strQty];
        
        [_dataArraySearch addObject:objCell];
        
        [_listOfMarc addObject:strTitleLabel];
    }
    //------------------------------------------------------
    //---create the index---
    [_stateIndex removeAllObjects];
    _stateIndex = [[NSMutableArray alloc] init];
    
    NSArray *array = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"Ñ",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z" ,nil];
    
    [_stateIndex addObjectsFromArray:array];
    //------------------------------------------------------
    
    [self.tableViewSearch reloadData];
}
*/

- (NSString *)stringByRemovingControlCharacters: (NSString *)inputString
{
    NSCharacterSet *controlChars = [NSCharacterSet controlCharacterSet];
    NSRange range = [inputString rangeOfCharacterFromSet:controlChars];
    if (range.location != NSNotFound) {
        NSMutableString *mutable = [NSMutableString stringWithString:inputString];
        while (range.location != NSNotFound) {
            [mutable deleteCharactersInRange:range];
            range = [mutable rangeOfCharacterFromSet:controlChars];
        }
        return mutable;
    }
    return inputString;
}

- (void) retrieveDataMarcas{
    
    [_dataArraySearch removeAllObjects];
    _dataArraySearch = [[NSMutableArray alloc] init];
    
    [_listOfMarc removeAllObjects];
    _listOfMarc = [[NSMutableArray alloc] init];
    
    NSString *strUrl = [NSString stringWithFormat:@"http://mobileecommerce.elasticbeanstalk.com/MobileJsonHome.aspx?strJsonBase=JsonMarcasProductos"];
    
    NSURL *url = [NSURL URLWithString:strUrl];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection
     sendAsynchronousRequest:urlRequest
     queue:[NSOperationQueue mainQueue]
     completionHandler:^(NSURLResponse *response,
                         NSData *data,
                         NSError *error)
     {
         if ([data length] >0 && error == nil)
         {
             NSLog(@"Load Home marcas perfecto!!!");
             
             NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
             
             newStr = [self stringByRemovingControlCharacters:newStr];
             
             NSData* dataUTF8 = [newStr dataUsingEncoding:NSUTF8StringEncoding];
             
             //[{"idm":"2", "dmp":"otto", "img":"2.jpg" }, {"idm":"3", "dmp":"otta", "img":"3.jpg" }, {"idm":"4", "dmp":"stanley ", "img":"4.jpg" }, {"idm":"5", "dmp":"teka ", "img":"5.jpg" }, {"idm":"6", "dmp":"polpaico", "img":"6.jpg" }, {"idm":"7", "dmp":"mademsa", "img":"7.jpg" }, {"idm":"8", "dmp":"fensa", "img":"8.jpg" }]
             
             jsonMarcas = [NSJSONSerialization JSONObjectWithData:dataUTF8 options:NSJSONReadingMutableLeaves error:nil];
             
             for (int i = 0; i<jsonMarcas.count; i++)
             {
                 NSString *strTitleLabel = [[jsonMarcas objectAtIndex:i] objectForKey:@"dmp"];
                 NSString *strSubtitleLabel = @"";
                 NSString *strTrademark = [[jsonMarcas objectAtIndex:i] objectForKey:@"dmp"];
                 NSString *strIdmark = [[jsonMarcas objectAtIndex:i] objectForKey:@"idm"];
                 
                 NSString *strSKU = @"";
                 NSString *strIdProducto = [[jsonMarcas objectAtIndex:i] objectForKey:@"idm"];
                 NSString *strReviews = @"";
                 NSString *strPrice = @"";
                 NSString *strOfferPrice = @"";
                 NSString *strPriceDuplicated = @"";
                 NSString *strDiscount = @"";
                 NSString *strOfferTime = @"";
                 NSString *strQty = @"";
                 
                 classGenericCell *objCell = [[classGenericCell alloc] initClassCell:strTitleLabel andSubtitleLabel:strSubtitleLabel andTrademark:strTrademark andIdMark:strIdmark andSKU:strSKU andStrIdProducto:strIdProducto andReviews:strReviews andstrPrice:strPrice andOfferPrice:strOfferPrice andstrPriceDuplicated:strPriceDuplicated andDiscount:strDiscount andOfferTime:strOfferTime andQty:strQty];
                 
                 [_dataArraySearch addObject:objCell];
                 
                 [_listOfMarc addObject:strTitleLabel];
             }
             
             //------------------------------------------------------
             //---create the index---
             [_stateIndex removeAllObjects];
             _stateIndex = [[NSMutableArray alloc] init];
             
             NSArray *array = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"Ñ",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z" ,nil];
             
             [_stateIndex addObjectsFromArray:array];
             //------------------------------------------------------
             
             [self.tableViewSearch reloadData];
             
         }
         else if ([data length] == 0 && error == nil)
         {
             NSLog(@"Nothing was downloaded.");
             
         }
         else if (error != nil){
             
             NSLog(@"Error = %@", error);
         }
     }];
    
}


-(NSString*) formatCurrencyValue:(double)value
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setCurrencySymbol:@"$"];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSNumber *c = [NSNumber numberWithFloat:value];
    return [numberFormatter stringFromNumber:c];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"iphone_header_solo.png"] forBarMetrics:UIBarMetricsDefault];
    
    self.tableViewSearch.hidden = YES;
    self.tableViewOrder.hidden = YES;
    
    objSliderPriceFilter.userInteractionEnabled = YES;
    
    [self hideTabBar];
    
    //------------------------------------------------------------------------------
    //Obtenemos lo
    dao *objDAO = [[dao alloc] init];
    int intIdUsuario = [objDAO obtainCurrentIdUser];
    
    if (intIdUsuario == -1) {
        intIdUsuario = 1;
    }
    
    if ([objDAO obtainExistUserFilterPrice:intIdUsuario] > 0) {
        //Al borrar los filtros se debe recargar la grilla de precios para lenar las variables de filtros de precios
        
        NSArray *arrayFilterPrice = [objDAO obtainUserFilterPrice:intIdUsuario];
        
        NSLog(@"arrayFilterPrice[0]: %@", arrayFilterPrice[0]);
        NSLog(@"arrayFilterPrice[1]: %@", arrayFilterPrice[1]);
        NSLog(@"arrayFilterPrice[2]: %@", arrayFilterPrice[2]);
        
        double dblPriceFrom = [arrayFilterPrice[0] doubleValue];
        double dblPriceTo = [arrayFilterPrice[1] doubleValue];
        double dblPriceCurrent = [arrayFilterPrice[2] doubleValue];
        
        lblPriceFrom.text = [self formatCurrencyValue:dblPriceFrom];
        lblPriceTo.text = [self formatCurrencyValue:dblPriceTo];
        lblPrice.text = [self formatCurrencyValue:dblPriceCurrent];
        
        objSliderPriceFilter.minimumValue = [arrayFilterPrice[0] floatValue];
        objSliderPriceFilter.maximumValue = [arrayFilterPrice[1] floatValue];
        objSliderPriceFilter.value = [arrayFilterPrice[2] floatValue];
    }
    
    [self retrieveDataOrder];
    //------------------------------------------------------------------------------
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [objSliderPriceFilter addTarget:self action:@selector(sliderDidEndSliding:) forControlEvents:(UIControlEventTouchUpInside | UIControlEventTouchUpOutside)];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (IBAction)backFilterAction:(id)sender {
    
//      [self.navigationController popToRootViewControllerAnimated:YES];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    
    //1.-Búsqueda por departamento
    NSString *strIdDepartment = [prefs stringForKey:@"strIdDepartment"];
    
    if (![strIdDepartment  isEqual: @"0"]) {

        //Volver desde la acion de búsqueda por Categorias
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];
        
    }else{
        //Volver desde la acion de búsqueda por palabras
        [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    }
    
}

- (IBAction)applyFilterAction:(id)sender {
    
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
}

- (IBAction)callNumber:(id)sender {
    
    NSString *strContactPhone;
    
    dao *objDAO = [[dao alloc] init];
    
    strContactPhone = [NSString stringWithFormat:@"tel://%@", [objDAO obtainCallCenterNumber]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strContactPhone]];
    
}

- (IBAction)btnPriceFilter:(id)sender {
    
    self.viewPriceFilter.hidden = NO;
    self.tableViewSearch.hidden = YES;
    
    [self.viewMarc setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [self.viewPrice setBackgroundColor:[UIColor darkGrayColor]];
    
    [self.btnPrice setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnMarc setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
}

- (IBAction)btnMarcFilter:(id)sender {
    
    self.viewPriceFilter.hidden = YES;
    self.tableViewSearch.hidden = NO;
    [self retrieveDataMarcas];
    
    [self.viewPrice setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [self.viewMarc setBackgroundColor:[UIColor darkGrayColor]];
    
    [self.btnMarc setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btnPrice setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    
    dao *objDAO = [[dao alloc] init];
    
    int intIdUsuario = [objDAO obtainCurrentIdUser];
    
    if (intIdUsuario == -1) {
        intIdUsuario = 1;
    }
    
    if ([objDAO obtainUserFilterMarc:intIdUsuario] > 0 ) {
        
        //-----------------------------------------------------------------
        //Marcamos la fila seleccionada en el sistema en base a row y section de indexPath
        NSArray *arrayFilterMarc = [objDAO obtainUserFilterMarcData:intIdUsuario];
        
        NSLog(@"arrayFilterPrice[0]: %@", arrayFilterMarc[0]);
        NSLog(@"arrayFilterPrice[1]: %@", arrayFilterMarc[1]);
        NSLog(@"arrayFilterPrice[2]: %@", arrayFilterMarc[2]);
        
        NSInteger intIdRow = [arrayFilterMarc[1] integerValue];
        NSInteger intIdSection = [arrayFilterMarc[2] integerValue];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:intIdRow inSection:intIdSection];
        
        [tableViewSearch selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
    
}

- (IBAction)changeSlider:(id)sender {
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    
    formatter.minimumFractionDigits = 3;
    formatter.maximumFractionDigits = 0; //8
    formatter.usesSignificantDigits = NO;
    formatter.usesGroupingSeparator = YES;
    formatter.groupingSeparator = @".";
    formatter.decimalSeparator = @".";
    
    NSString *formattedNumber = [formatter stringFromNumber:[NSNumber numberWithFloat:objSliderPriceFilter.value]];
    
    lblPrice.text= [[NSString alloc] initWithFormat:@"$%@", formattedNumber];
}

- (void)sliderDidEndSliding:(NSNotification *)notification {
    
    NSLog(@"Slider did end sliding... Do your stuff here");
    //------------------------------------------------------------------------------
    //Obtenemos lo
    dao *objDAO = [[dao alloc] init];
    int intIdUsuario = [objDAO obtainCurrentIdUser];
    
    if (intIdUsuario == -1) {
        intIdUsuario = 1;
    }
    
    //-----------------------------------------------------------------
    //Almacenamos los valores desde y hasta de los filtros de precios
    NSArray *arrayFilterPrice = [objDAO obtainUserFilterPrice:intIdUsuario];
    
    NSLog(@"arrayFilterPrice[0]: %@", arrayFilterPrice[0]);
    NSLog(@"arrayFilterPrice[1]: %@", arrayFilterPrice[1]);
    NSLog(@"arrayFilterPrice[2]: %@", arrayFilterPrice[2]);
    
    double dblPriceFrom = [arrayFilterPrice[0] doubleValue];
    double dblPriceTo = [arrayFilterPrice[1] doubleValue];
    //  double dblPriceCurrent = [arrayFilterPrice[2] doubleValue];
    
    [objDAO deleteUserFilterPrice:intIdUsuario];
    
    [objDAO insertUserFilterPrice:intIdUsuario intPriceDesde:dblPriceFrom intPriceHasta:dblPriceTo intPriceCurrent:objSliderPriceFilter.value];
    
    //-----------------------------------------------------------------
}


@end